package org.mclll.xwangplugin.town.command.executor;

import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;
import org.mclll.xwangplugin.town.tax.TownTax;

import static org.mclll.xwangplugin.town.config.TownContext.*;

public class TownSubmitTax {

    public static void submitTax(Player player, String[] args) {
        if (args.length != 2) {
            player.sendMessage(TOWN_SUBMIT_TAX_FORMAT);
            return;
        }
        if (!StringUtils.isNumeric(args[1])) {
            player.sendMessage(String.format(NUMBER_PARSE_ERROR, args[1]));
        }
        Integer taxCount = Integer.valueOf(args[1]);
        TownPlayerDO townPlayer = TownPlayerManage.selectByUuid(player.getUniqueId().toString());
        if (townPlayer.getIdentity() == PlayerTownIdentity.GENSHIN) {
            player.sendMessage(NOT_PERMISSION);
            return;
        }
        if (TownTax.submitTax(player, TownManage.selectByUuid(townPlayer.getTownUuid()), taxCount)) {
            player.sendMessage(String.format(TOWN_SUBMIT_TAX_SUCCESS, taxCount));
        } else {
            player.sendMessage(NO_HAVE_HEART_STONE);
        }
    }
}
